import 'package:flutter/material.dart';
import 'listData.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(primarySwatch: Colors.pink),
        home: Scaffold(
          appBar: AppBar(title: Text('动态列表')),
          body: HomePage(),
        ));
  }
}

class HomePage extends StatelessWidget {
  // 方法1：自定义方法(直接调用当前方法，返回一个列表)
  /*final int count = 20;
  final List<Widget> listData = new List();
  List<Widget> _getData() {
    for(int i = 0; i< count; i++) {
      listData.add(
        ListTile(
          title: Text('标题$i')
        )
      );
    }
    return listData;
  }*/
  // 方法2：从数据返回
  List<Widget> _getData2() {
    var tempList = tempData.map((value) {
      return ListTile(
        leading: Image.network(value['imageUrl']),
        title: Text(value['title']),
        subtitle: Text(value['author']),
      );
    });
    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    // 动态列表(用循环返回一个List)
    /* return ListView(
        children: this._getData2()
    );*/
    
    // 动态列表(用ListBuilder)
    return ListView.builder(
        itemCount: tempData.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(tempData[index]['title']),
            subtitle: Text(tempData[index]['author']),
            trailing: Image.network(tempData[index]['imageUrl']),
          );
        });
  }
}
